#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash ann-1.1.2.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then
    echo "请输入正确的构建路径"
    exit 1
fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then
    echo "请输入正确的安装路径"
    exit 1
fi

buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
yum install -y wget tar cmake environment-modules
wget -t 10 -c -P $buildpath http://www.cs.umd.edu/~mount/ANN/Files/1.1.2/ann_1.1.2.tar.gz

tar xf $buildpath/ann_1.1.2.tar.gz -C $buildpath && rm -rf $buildpath/ann_1.1.2.tar.gz
cd $buildpath/ann_1.1.2
sed -i '74s/C++ = g++/C++ = clang++/g' Make-config
sed -i '583s/main/int main/g' ann2fig/ann2fig.cpp
make linux-g++
cp -r $buildpath/ann_1.1.2 $installpath

cat >"$installpath/ann_1.1.2/ann_modulefiles" <<EOF
#%Module1.0
conflict ann
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
        set modfile_path [file dirname \$modfile]
            return \$modfile_path
    }
set pwd [getModulefileDir]
set ANN \$pwd
setenv ANN \$ANN
prepend-path ANN_DIR \$ANN
EOF
rm -rf $buildpath/ann_1.1.2
